package com.mec.web.y2012.coreservlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.mec.beans.y2012.coreservlet.BankCustomer;

@WebServlet("/ShowBalanceServlet")
public class ShowBalanceServlet extends HttpServlet {
	
	private static final long serialVersionUID = -8270309869347403792L;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		BankCustomer customer = BankCustomer.getCustomer(req.getParameter("id"));
		String address = "/WEB-INF/jsp/";
		//
		if(null == customer){
			address += "unknownCustomer.jsp";
		}else if(customer.getBalance() < 0 ){
			address += "negativeBalance.jsp";
			req.setAttribute("badCustomer", customer);
		}else if(customer.getBalance() < 10000 ){
			address += "normalBalance.jsp";
			req.setAttribute("regularCustomer", customer);
		}else{
			address += "highBalance.jsp";
			req.setAttribute("eliteCustomer", customer);
		}
		
		//
		RequestDispatcher dispatcher = req.getRequestDispatcher(address);
		dispatcher.forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}



	private static final Logger logger = 
			Logger.getLogger(ShowBalanceServlet.class);

}
